<template>
  <div>
    <div class="box" v-if="data.options">
      <div class="left">
        <p>单程：{{data.info.departCity}} - {{data.info.destCity}} / {{data.info.departDate}}</p>
      </div>
      <div class="right">
        <!-- 起飞机场 -->
        <el-select
          @change="runFilter"
          style="width:120px;"
          size="mini"
          v-model="depNameValue"
          placeholder="起飞机场"
        >
          <el-option
            v-for="(item,index) in data.options.airport"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
        <!-- 起飞时间 -->
        <el-select
          @change="runFilter"
          style="width:120px;"
          size="mini"
          v-model="depTimeValue"
          placeholder="起飞时间"
        >
          <el-option
            v-for="(item,index) in data.options.flightTimes"
            :key="index"
            :label="`${item.from}:00-${item.to}:00`"
            :value="`${item.from}:00-${item.to}:00`"
          ></el-option>
        </el-select>
        <!-- 航空公司 -->
        <el-select
          @change="runFilter"
          style="width:120px;"
          size="mini"
          v-model="airlineValue"
          placeholder="航空公司"
        >
          <el-option
            v-for="(item,index) in data.options.company"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
        <!-- 机型 -->
        <el-select
          @change="runFilter"
          style="width:120px;"
          size="mini"
          v-model="airTypeValue"
          placeholder="机型"
        >
          <el-option
            v-for="(item,index) in airType"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
    </div>
    <div class="filter">
      <div class="left">
        <p>筛选：</p>
        <div @click="clear">撤销</div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      airType: [
        { value: 'L', label: '大' },
        { value: 'M', label: '中' },
        { value: 'S', label: '小' }
      ],
      depNameValue: '',
      depTimeValue: '',
      airlineValue: '',
      airTypeValue: ''
    }
  },
  methods: {
    runFilter() {
      let arr = this.data.flights
      // 过滤起飞机场
      if (this.depNameValue) {
        arr = arr.filter(x => {
          return x.org_airport_name === this.depNameValue
        })
        this.$emit('filter', arr)
      }
      // 过滤起飞时间
      if (this.depTimeValue) {
        const newArr = this.depTimeValue.split('-')
        const start = +newArr[0].split(':')[0]
        const end = +newArr[1].split(':')[0]
        arr = arr.filter(x => {
          let time = +x.dep_time.split(':')[0]
          if (+x.dep_time.split(':')[1] >= 0) {
            time += 0.1
          }
          return time > start && time < end
        })
        this.$emit('filter', arr)
      }
      // 过滤航空公司
      if (this.airlineValue) {
        arr = arr.filter(x => {
          return x.airline_name === this.airlineValue
        })
        this.$emit('filter', arr)
      }
      // 过滤机型
      if (this.airTypeValue) {
        arr = arr.filter(x => {
          return x.plane_size === this.airTypeValue
        })
        this.$emit('filter', arr)
      }
    },
    clear() {
      this.depNameValue = ''
      this.depTimeValue = ''
      this.airlineValue = ''
      this.airTypeValue = ''
      this.$emit('filterClear')
    }
  }
}
</script>
<style lang="less" scoped>
.box {
  display: flex;
  justify-content: space-between;
  .left {
    flex: 1;
    display: flex;
    align-items: center;
    height: 30px;
    // background: red;
    p {
      font-size: 14px;
    }
  }
}
.filter {
  margin-top: 10px;
  .left {
    display: flex;
    align-items: center;
    height: 30px;
    p {
      font-size: 14px;
    }
    div {
      height: 28px;
      padding: 0 15px;
      line-height: 26px;
      background: #ecf5ff;
      border: 1px solid #409eff;
      border-radius: 14px;
      font-size: 12px;
      color: #409eff;
      cursor: pointer;
      &:hover {
        background: #409eff;
        color: #fff;
      }
    }
  }
}
</style>